using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.ServiceManager
{
	public class CityManager
	{
		#region CONSTRUCTORS
		static CityManager()
		{
            _data = new CityData(Util.GetConnectionString("SMSSERVICE"));
			_error = "";
		}
		#endregion
		
		#region ATTRIBUTES
		private static CityData _data;
		private static string _error;
		#endregion
		
		#region PROPERTIES
		public static string ErrorMessage
		{
			get
			{
				return _error;
			}
		}
		
		#endregion
		
		#region METHODS
		public static bool Insert( string cityName,  string cityCode,  int countryID)
		{
			_error = "";
			try
			{
				object[] values = new object[3];
				values[0] = Util.ReplaceNullTo(cityName, DBNull.Value);
				values[1] = Util.ReplaceNullTo(cityCode, DBNull.Value);
				values[2] = countryID;
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Insert( CityInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[3];
				values[0] = Util.ReplaceNullTo(info.CityName, DBNull.Value);
				values[1] = Util.ReplaceNullTo(info.CityCode, DBNull.Value);
				values[2] = info.CountryID;
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( int cityID,  string cityName,  string cityCode,  int countryID)
		{
			_error = "";
			try
			{
				object[] values = new object[4];
				values[0] = cityID;
				values[1] = Util.ReplaceNullTo(cityName, DBNull.Value);
				values[2] = Util.ReplaceNullTo(cityCode, DBNull.Value);
				values[3] = countryID;
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( CityInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[4];
				values[0] = info.CityID;
				values[1] = Util.ReplaceNullTo(info.CityName, DBNull.Value);
				values[2] = Util.ReplaceNullTo(info.CityCode, DBNull.Value);
				values[3] = info.CountryID;
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( int cityID)
		{
			_error = "";
			try
			{
				object[] values = { cityID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( CityInfo info)
		{
			_error = "";
			try
			{
				object[] values = { info.CityID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static CityInfo Select( int cityID)
		{
			_error = "";
			try
			{
				object[] values = { cityID };
				DataTable dTable = _data.Select(values);
				if (dTable.Rows.Count < 1) return null;
		
				
				CityInfo info = new CityInfo(dTable.Rows[0]);
				return info;
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable SelectAll()
		{
			_error = "";
			try
			{
				return _data.SelectAll();
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder,  int pageIndex,  int pageSize)
		{
			_error = "";
			try
			{
				object[] values = { whereClause, sortOrder };
				int index = (pageIndex * pageSize) - 1;
				if (index < 0) index = 0;
				return _data.Search(values, index, pageSize);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder)
		{
			return CityManager.Search(whereClause, sortOrder, 0, 0);
		}
		
		public static DataTable Search( int pageIndex,  int pageSize)
		{
			return CityManager.Search( "", "", pageIndex, pageSize);
		}
		
		#endregion
		
	}
}